package redis

import (
	"context"
	"encoding/json"
	"fmt"
	"gin_study_blog/common"
	"time"
)

const newsIndex = "news_index"

type NewData struct {
	Index    string `json:"index"`
	Title    string `json:"title"`
	HotValue string `json:"hotValue"`
	Link     string `json:"link"`
}

// SetNews 设置某一个数据, 重复执行, 重复累加
func SetNews(key string, NewData []NewData) error {
	jsonNewData, _ := json.Marshal(NewData)
	err := common.Redis.Set(context.Background(), fmt.Sprintf("%s_%s", newsIndex, key), jsonNewData, 3*time.Hour).Err()
	return err
}

func GetNews(key string) (NewData []interface{}, err error) {
	res := common.Redis.Get(context.Background(), fmt.Sprintf("%s_%s", newsIndex, key)).Val()
	err = json.Unmarshal([]byte(res), &NewData)
	return NewData, err
}
